﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RushResetConfirmPanel : MonoBehaviour
    {
        public UILabel detailLabel;
        public UILabel resetNumLabel;

        public RushPage rushPage;

        public List<UITweener> tweeners;

        public enum ResetType
        {
            Normal,
            Diamond
        }

        ResetType currentResetType;
        int time;
        int cost;

        public void Enter(int leftTime) {
            currentResetType = ResetType.Normal;
            time = leftTime;
            cost = 0;
            detailLabel.text = string.Format(TableTextBlo.GetText("rush_reset"), "");
            resetNumLabel.text = time.ToString();

            gameObject.SetActive(true);
            foreach (var tweener in tweeners) {
                if (tweener != null) {
                    tweener.ResetToBeginning();
                    tweener.PlayForward();
                }
            }
        }

        public void Enter(int leftTime, int costNum)
        {
            currentResetType = ResetType.Diamond;
            time = leftTime;
            cost = costNum;
            string costStr = string.Format(TableTextBlo.GetText("rush_diamond_reset_pre"), costNum);
            detailLabel.text = string.Format(TableTextBlo.GetText("rush_reset"), costStr);
            resetNumLabel.text = time.ToString();

            gameObject.SetActive(true);
            foreach (var tweener in tweeners)
            {
                if (tweener != null)
                {
                    tweener.ResetToBeginning();
                    tweener.PlayForward();
                }
            }
        }

        public void Exit() {
            gameObject.SetActive(false);
        }

        public void OnConfirmButtonClick() {
            if (currentResetType == ResetType.Diamond)
            {
                if (StaticData.playerData.diamond < cost)
                {
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("diamond_lack"));
                    return;
                }
            }

            ProtoBuf.Message.CSRushResetChallengeCount msg = new ProtoBuf.Message.CSRushResetChallengeCount();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSRushResetChallengeCount, msg, (pack, b) => {
                if (b)
                {
                    ProtoBuf.Message.SCRushResetChallengeCount _msg = pack.kBody as ProtoBuf.Message.SCRushResetChallengeCount;
                    StaticData.playerData.Delete(_msg.cost);
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("rush_reset_success"));
                    Exit();
                    rushPage.OnCoverPageRemove();
                    if (rushPage.onRushResetConfirm != null)
                    {
                        rushPage.onRushResetConfirm();
                    }
                }
            });

        }

        public void OnCancelButtonClick() {
            Exit();
        }

    }
}